﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Product App</title>
    <style>
        .link
        {
            stroke: #000;
            stroke-width: 1.5px;
        }
        
        .node
        {
            cursor: move;
            fill: #ccc;
            stroke: #000;
            stroke-width: 1.5px;
        }
        
        .node.fixed
        {
            fill: #f00;
        }
    </style>
</head>
<body>
    <script src="Scripts/d3/d3.v3.min.js" type="text/javascript"></script>
    <script type="text/javascript">
        //var uri = 'http://localhost:36828/ArtefactsLinksService.svc/GetPastDays/days=2';
        //var uri = 'http://localhost:36828/ArtefactsLinksService.svc/GetByInstanceID/InstanceID=79a2dfc3-a6ff-4686-a114-3b14b8d1f5f5';
        var uri = 'http://localhost:36828/ArtefactsLinksService.svc/GetByInstanceID/InstanceID=a0e9d694-0ca4-499f-b7b7-70a7c188b953';
        var width = 960, height = 500;

        var force = d3.layout.force()
    .size([width, height])
    .charge(-400)
    .linkDistance(40)
    .on("tick", tick);

        var drag = force.drag()
    .on("dragstart", dragstart);

        var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

        var link = svg.selectAll(".link"),
    node = svg.selectAll(".node");

        d3.json(uri, function (error, graph) {

            var nodes = [],
            nodeIndexByArtefactId = {},
            links = [];
            graph.Artefacts.forEach(function (artefact) {
                nodeIndexByArtefactId["" + artefact.Id] = nodes.length;
                nodes.push(artefact);
            });
            graph.Links.forEach(function (link) {
                links.push({
                    source: nodeIndexByArtefactId["" + link.FromComponentId],
                    target: nodeIndexByArtefactId["" + link.ToComponentId]
                });
            });

            force
        .nodes(nodes)
        .links(links)
        .start();

            link = link.data(links)
    .enter().append("line")
    .attr("class", "link");

            node = node.data(nodes)
    .enter().append("circle")
    .attr("class", "node")
    .attr("r", 12)
    .on("dblclick", dblclick)
    .call(drag);
        });

        function tick() {
            link.attr("x1", function (d) { return d.source.x; })
      .attr("y1", function (d) { return d.source.y; })
      .attr("x2", function (d) { return d.target.x; })
      .attr("y2", function (d) { return d.target.y; });

            node.attr("cx", function (d) { return d.x; })
      .attr("cy", function (d) { return d.y; });
        }

        function dblclick(d) {
            d3.select(this).classed("fixed", d.fixed = false);
        }

        function dragstart(d) {
            d3.select(this).classed("fixed", d.fixed = true);
        }
    </script>
</body>
</html>
